<template>
	<view class="">
		<u--form class="form" labelPosition="left" :model="model1" :rules="rules" ref="form1">
			
			<u-form-item class="inp" prop="userInfo.old" borderBottom ref="item1">
				<u--input placeholder='请输入原始密码' v-model="model1.userInfo.old" border="none"></u--input>
			</u-form-item>
			<u-form-item class="inp" prop="userInfo.new1" borderBottom ref="item1">
				<u--input placeholder='请输入新密码' v-model="model1.userInfo.new1" border="none" type="password"></u--input>
			</u-form-item>
			<u-form-item class="inp" prop="userInfo.new2" borderBottom ref="item1">
				<u--input placeholder='请确认新密码' v-model="model1.userInfo.new2" border="none" type="password"></u--input>
			</u-form-item>
			
		</u--form>
		<button @click="submit">确认修改密码</button>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				model1: {
					userInfo: {
						old: '',
						new1: '',
						new2:''
					},
				},
				rules: {
					'userInfo.old': {
						type: 'string',
						required: true,
						message: '请填写旧密码',
						trigger: ['blur', 'change']
					},
					'userInfo.new1': {
						type: 'string',
						required: true,
						message: '请填写新密码',
						trigger: ['blur', 'change']
					},
					'userInfo.new1': {
						type: 'string',
						required: true,
						message: '请填写确认新密码',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		methods:{
			//修改密码
			editPass:async function(id,oldPassWorld,newPassWorld){
				let params={
					id,
					oldPassWorld,
					newPassWorld
				}
				const res = await this.$api.editPass(params)
				console.log(res)
				if(res.code==500){
					alert(res.msg)
				}else{
					localStorage.removeItem('user')
					localStorage.removeItem('token')
					this.$tool.editPass()
					
					setTimeout(function(){
						uni.navigateTo({
						url:'../login/login'
					})
					},500)
				}
			},
			submit(){
				let {new1,new2,old}=this.model1.userInfo
				let id=JSON.parse(localStorage.getItem('user')).id
				console.log(id,new1,new2,old)
				if(new1==new2){
					this.editPass(id,old,new1)
				}else{
					alert('两次输入密码不一样')
				}
			}
		}
	}
</script>

<style>
	.form{
		margin: 20rpx;
	}
	button {
		background-color: rgb(118,237,214);
		color: white;
		width: 50%;
		margin: 50rpx auto;
	}
	.inp{
		margin: 20rpx;
	}
</style>